# -*- coding: utf-8 -*-

import mod.client.extraClientApi as clientAPI
from mod.client.system.clientSystem import ClientSystem

from dwjjCoreClientScripts.common import consts


class PlayerModule(object):
    # 禁用物品列表
    itemBanList = set()

    def __init__(self, clientSystem):
        # type: (ClientSystem) -> None
        self.clientSystem = clientSystem
        self.Init()
        self.InitComp()
        self.SetListener()

    def Init(self):
        self.localPlayerId = clientAPI.GetLocalPlayerId()

    def InitComp(self):
        self.gameComp = clientAPI.GetEngineCompFactory().CreateGame(clientAPI.GetLevelId())

    def SetListener(self):
        # 客户端加载事件
        self.clientSystem.ListenForEvent(clientAPI.GetEngineNamespace(), clientAPI.GetEngineSystemName(), "OnLocalPlayerStopLoading", self, self.OnLocalPlayerStopLoading, 0)
        # 来自服务端的配置信息回复
        self.clientSystem.ListenForEvent(consts.ServerModName, consts.ServerSystemName, consts.PlayerModuleConfigResponse, self, self.OnConfigResponse, 0)
        # 屏幕点击事件 限制类的操作暂时由该事件处理
        self.clientSystem.ListenForEvent(clientAPI.GetEngineNamespace(), clientAPI.GetEngineSystemName(), "TapBeforeClientEvent", self, self.OnHandLimitItemTapBefore, 0)
        self.clientSystem.ListenForEvent(clientAPI.GetEngineNamespace(), clientAPI.GetEngineSystemName(), "HoldBeforeClientEvent", self, self.OnHandLimitItemTapBefore, 0)
        self.clientSystem.ListenForEvent(clientAPI.GetEngineNamespace(), clientAPI.GetEngineSystemName(), "RightClickBeforeClientEvent", self, self.OnHandLimitItemTapBefore, 0)
        self.clientSystem.ListenForEvent(clientAPI.GetEngineNamespace(), clientAPI.GetEngineSystemName(), "LeftClickBeforeClientEvent", self, self.OnHandLimitItemTapBefore, 0)

    # 客户端加载事件 触发配置获取请求
    def OnLocalPlayerStopLoading(self, e):
        if e['playerId'] != self.localPlayerId:
            return
        self.clientSystem.NotifyToServer(consts.PlayerModuleConfigRequest, {'playerId': self.localPlayerId})

    # 配置文件信息回复
    def OnConfigResponse(self, e):
        # 初始化禁用物品列表
        self.itemBanList.clear()
        for v in e["itemBanList"]:
            self.itemBanList.add(v)

    # 屏幕的点击事件  限制使用物品的操作暂时由该事件处理
    def OnHandLimitItemTapBefore(self, e):
        itemComp = clientAPI.GetEngineCompFactory().CreateItem(self.localPlayerId)

        # 获取右手物品的名称
        rightItemDict = itemComp.GetCarriedItem(False)
        rightItemName = "None"
        if rightItemDict:
            rightItemName = rightItemDict["newItemName"]

        # 获取左手物品的名称
        leftItemDict = itemComp.GetOffhandItem(False)
        leftItemName = "None"
        if leftItemDict:
            leftItemName = leftItemDict["newItemName"]

        # 禁用物品列表
        if rightItemName in self.itemBanList or leftItemName in self.itemBanList:
            e["cancel"] = True
